--
-- 坐骑
--

local Skill = import(".Skill")
local Mount = class("Mount")

function Mount:ctor()
	self.itemBagType = Enums.ItemBagType.mount
    self.showType = Enums.ItemShowType.mount

    -- 因为坐骑的唯一性，uid即为tid
    self.uid = 0
    self.tid = 0
    self.duration = 0
    self.levelID = 0
    self.starID = 0

    -- 激活态坐骑的坑位置，未激活都为0
    self.index = 0
    

	self.isNew = false
    -- 是否拥有
    self._isHave = false 
end

function Mount:isValid()
    return self.conf ~= nil
end

function Mount:getAmount()
    return 1
end

function Mount:read(value)
    self.uid = value.uid
    self.tid = value.tid
    self.duration = value.duration or 0
    -- db init
    local conf = GD:getMountConfig(self.tid)
    local resConf = GD:getMountResConfig(self.tid)
    assert(conf ~= nil, "Mount.conf can't be nil")
    assert(resConf ~= nil, "Mount.resConf can't be nil")
    self.conf = conf
    self.resConf = resConf

    -- 只是id，用来读表的
    self.starID = value.starID
    if nil ~= self.starID and self.starID > 0 then
        self.starConf = GD:getMountStarConfig(self.starID)
    else
        self.starConf = GD:getMountStarConfByStar(self.tid, tonumber(conf.initStar or 1))
        self.starID = tonumber(self.starConf.ID)
    end
    -- 金腰带坐骑 星级 配0   mountstar 找不到配置 构造一个空的 去支持(由于策划 填写id 连续 插入 配置 0级坐骑不方便)
    if  not self.starConf and self:isSpRewardMount() then
        self.starConf = {}
        local startConf= GD:getMountStarConfByStar(self.tid, 1)
        self.starConf.ID = startConf.ID
        self.starConf.currentStar = 0
        self.starConf.attribute1Type = 1
        self.starConf.attribute1Value = 0
        self.starConf.mountID = startConf.mountID
        self.starConf.material1 = ""
        self.starConf.material2 = ""
        self.starConf.material3 = ""
        self.starConf.material4 = ""
        self.starConf.mountName  = startConf.mountName
        self.starConf.requireLevel = 0
        self.starID = tonumber(self.starConf.ID)
    end
    assert(self.starConf ~= nil, "Mount.starConf can't be nil")

    -- 星级，品质
    self.star = tonumber(self.starConf.currentStar)
    self.quality = value.levelID or tonumber(conf.initRank or 1)
    self.maxStar = tonumber(conf.maxStar or 5)
    self.maxQuality = tonumber(conf.maxRank or 5)

    self.soulID = tonumber(conf.soulID)
    self.teamSkillID = tonumber(conf.teamSkillID or 0)

    self._size = nil
    self._icon = nil
    self._iconBig = nil

    self._starCostItems = nil
    self._starAttrs = nil
    self._shareData = nil
    self._skill = nil
    self._nextSkill = nil
    self._skillId = nil
end

function Mount:setIndex(index)
    self.index = index
end

function Mount:getSkillId()
    if not self._skillId then
        self._skillId = self.teamSkillID * 100 + self.quality
    end
    return self._skillId
end

function Mount:getSkill()
    if not self._skill then
        local skillId = self:getSkillId()
        self._skill = Skill.new()
        self._skill:read({uid=skillId})
    end
    return self._skill
end

function Mount:getSkillDesc()
    local skill = self:getSkill()
    return skill:getDes()
end

-- 返回skill属性
function Mount:getSkillAttr()
    local skill = self:getSkill()
    return skill:getAttrs()
end

-- 获得下个品质技能
function Mount:getNextSkill()
    if self:isMaxQuality() then return end

    if not self._nextSkill then
        local nextSkillId = self.teamSkillID * 100 + self.quality+1
        local skill = Skill.new()
        skill:read({uid=nextSkillId})
        self._nextSkill = skill
    end
    return self._nextSkill
end

function Mount:getStar()
    return self.star
end

function Mount:getUid()
	return self.uid
end

function Mount:getId()
	return self.tid
end

-- 检测道具是否有可以预览的外显
-- return 是否可以，预览参数
function Mount:isClassAvatarEnable(classId)
    return true, {type=Enums.AvatarPreviewType.mount, id=self.tid}
end

function Mount:isEnable()
    return self.conf.enable == "1"
end

function Mount:canEquip()
    return self.conf.poundenable == "1"
end

function Mount:canShowPassenger()
    return self.conf.showPassenger == "1"
end

function Mount:isRiding()
	return self.uid==UD.player.mountUID
end

function Mount:isActive()
	return UD:isActivatedMountEquiped(self.uid)
end

function Mount:getName()
    return L(self.conf.name)
end

-- 获取类型名字
function Mount:getTypeName()
    if self.conf.typeDesc then
        return L(self.conf.typeDesc)
    end

    return L(Res.showTypeName .. self.showType)
end

-- 缩放
function Mount:getSize()
    if not self._size then
        self._size = tonumber(self.resConf.size or 100)/100
    end
    return self._size
end

function Mount:getQuality()
	return self.quality
end

function Mount:isMaxQuality()
	return self.maxQuality == self.quality
end

function Mount:isMaxStar()
	return self.maxStar == self.star
end

function Mount:getStarUpNeedLv()
    return tonumber(self.starConf.requireLevel)
end

-- 星星附带属性(当前仅有一条属性)
function Mount:getStarAttrs()
    if not self._starAttrs then
        self._starAttrs = {0}
        for i=1,1 do
            local attr = {
                tonumber(self.starConf["attribute" .. i .. "Type"]),
                tonumber(self.starConf["attribute" .. i .. "Value"])
            }
            self._starAttrs[i] = attr
        end
    end
    return self._starAttrs
end

-- 坐骑附带的属性
function Mount:getAttrs()
    local ret = {}
    if tonumber(self.conf.poundenable) == 0 then
        return ret
    end

    self:getStarAttrs()

    for i,v in ipairs(self._starAttrs) do
        ret[v[1]] = v[2]
    end
    return ret
end

function Mount:getStarCostItems()
    if not self._starCostItems then
        self._starCostItems = {}
        for i=1,4 do
            local str = self.starConf["material"..i]
            if str then
                local item = UD:parseRewardToBagItem(str)
                self._starCostItems[#self._starCostItems + 1] = item
            end
        end
    end
    return self._starCostItems
end

-- 是否是相关的道具id
function Mount:isRelatedItem(itemId)
    local costItems = self:getStarCostItems()
    for _,item in ipairs(costItems) do
        if item.tid == itemId then
            return true
        end
    end
    return false
end

-- 是否是坐骑灵魂石道具id
function Mount:isSoulItem(soulId)
    return self.soulID == soulId
end

-- 可以被合成
function Mount:canBeComposed()
    return self.conf.composeFragment ~= "0"
end

function Mount:getQualityCostNum()
    local itemId = self.soulID
    local haveNum = UD:getMountSoulCountById(itemId)
    local needNum
    if self._isHave then
        if self.conf.qualityUpdate == "0" then
            needNum = 0
        else
            if not self._qualityUpAry then
                local ary = string.splitTwice(self.conf.qualityUpdate, nil, true)
                local ret = {}
                for i,v in ipairs(ary) do
                    ret[v[1]] = v[2]
                end
                self._qualityUpAry = ret
            end
            needNum = self._qualityUpAry[self.quality]
        end
    else
        needNum = tonumber(self.conf.composeFragment)
    end
    -- print("getQualityCostNum haveNum, needNum", haveNum, needNum)
    return haveNum, needNum
end

-- 解析来源
function Mount:getResourceList()
    if not self._resource then
        local ret = {}
        local resource = self:getResource()
        if resource then
            ret = string.splitTwice(resource, {";", "|"})
        end
        for _,v in ipairs(ret) do
            v[1] = tonumber(v[1])
            v[2] = L(v[2])
            if v[3] then
                v[3] = string.split(v[3], "#")
            end
        end
        self._resource = ret
    end
    return self._resource
end

-- 临时给tips统一处理用，应该统一用上面的getResource就可以了
function Mount:getResource()
    local resource = GD:getConvertResource(self.conf.getResource, Enums.ItemBagType.item, tonumber(self.conf.itemID))
	return resource
end

function Mount:getDesc()
	return L(self.conf.description)
end

function Mount:getIcon()
    if not self._icon then
       self._icon = string.format("%s%s.png", Res.mountIconPath, self.resConf.icon)
    end
    return self._icon
end

function Mount:getIconBig()
    if not self._iconBig then
       self._iconBig = string.format("%s%s.png", Res.mountIconPath, self.resConf.iconBig)
    end
    return self._iconBig
end

function Mount:getIconBg()
    return Res.Qulity_Icon_Bg[self.quality]
end

function Mount:getState()
    if self:haveMount() then
        if self:isActive() then
	        return L("activate")
        else
	        return L("unactivate")
        end
    else
	    return L("text_11800011")
    end
end

function Mount:haveMount()
	return self._isHave
end

function Mount:setHave(have)
	self._isHave = have
end

function Mount:setIsNewGet(isNew)
	self.isNew = isNew
end

function Mount:toJson()
	return json.encode({uid=self.uid, tid=self.tid})
end

function Mount:setShareData(shareData)
    self._shareData = shareData
end
function Mount:getShareData()
    return self._shareData
end

function Mount:checkActiveOp()
    return self._isHave and self:canEquip() and not self:isActive() and not UD:isActivatedMountFull()
end

-- 是否可以合成
function Mount:checkComposition()
    if not self._isHave then
        local haveNum, needNum = self:getQualityCostNum()
        return haveNum >= needNum and needNum ~= 0
    end
    return false
end

function Mount:checkQualityCostItemsEnough()
    local haveNum, needNum = self:getQualityCostNum()
    local isMaxQuality = self:isMaxQuality()
    return not isMaxQuality and haveNum >= needNum
end

function Mount:checkStarPlayerLevel()
    local needLv = self:getStarUpNeedLv() or 0
    local playerLv = UD.player.level
    return playerLv >= needLv
end

function Mount:checkStarCostItemsEnough()
    local items = self:getStarCostItems()
    for _, item in ipairs(items) do
        local needNum = item.amount or 1
        local hasNum = UD:getItemAmountByTid(item.tid)
        if hasNum < needNum then
            return false
        end
    end
    return true
end

-- 检查是否可以提升星级
function Mount:checkStarOp()
    if self:isMaxStar() then return false end

    return self:checkStarPlayerLevel() and self:checkStarCostItemsEnough()
end

-- 检查 是否可以 升星 或者 提升品质
function Mount:checkCanOperation()
    if not self:canEquip() then
        return false
    end
    if not self:haveMount() then
        return false
    end
    if self:checkQualityCostItemsEnough() or self:checkStarOp() then
        return true
    end
    return false
end

-- 判断是否是 金腰带坐骑
function Mount:isSpRewardMount()
    return tonumber(self.conf.type2 or 0) == 1
end

function Mount:getDuration()
    return self.duration
end

function Mount:isPoundEnable()
    return self.conf.poundenable == "1"
end

return Mount
